#ifndef __IIC_C
#define __IIC_C

#include "IIC.h"
#include "SQDriConfig.h"

void IIC_Pin_init(){
	  GPIO_Config_T  configStruct;
	  RCM_EnableAPB2PeriphClock(IIC2_PIN_RTC | IIC2_PinAF_RTC);
	  // I2C_SDA
 	  configStruct.mode = GPIO_MODE_AF_OD;
	  configStruct.speed = GPIO_SPEED_50MHz;
	  configStruct.pin = IIC2_SDA_PinId;
	  GPIO_Config(IIC2_PinPort, &configStruct);
	  // I2C_SCL
	  configStruct.mode = GPIO_MODE_AF_OD;
	  configStruct.speed = GPIO_SPEED_50MHz;
	  configStruct.pin = IIC2_SCL_PinId;
	  GPIO_Config(IIC2_PinPort, &configStruct);
}

void IIC_init(){	 
   I2C_Config_T i2cConfigStruct;
   RCM_EnableAPB1PeriphClock(IIC_RTC);
   I2C_Reset(IIC_ID);
   i2cConfigStruct.mode = I2C_MODE_I2C;
	 i2cConfigStruct.dutyCycle = I2C_DUTYCYCLE_2;//I2C_DUTYCYCLE_16_9; // I2C_DUTYCYCLE_2; // IIC占空比: 2 = 低位 / 高位
   i2cConfigStruct.ackAddress = I2C_ACK_ADDRESS_7BIT; // 应答地址7位
	 // 设备写地址：
	 i2cConfigStruct.ownAddress1 = My_Addr; // 主机地址
   i2cConfigStruct.ack = I2C_ACK_ENABLE;  // I2C_ACK_ENABLE  
   i2cConfigStruct.clockSpeed = 100000;   // IIC速度 = 400K;IIC最高速
   I2C_Config(IIC_ID,&i2cConfigStruct);  

	 // 开启DMA功能：
	 I2C_EnableDMA(IIC_ID);
	
   /* Enable I2Cx */
   I2C_Enable(IIC_ID);	 
	 
}



// IIC-DMA配置
void I2C_TX_DMA_Init(){
    DMA_Config_T dmaConfig;

    /* Enable DMA Clock */
    RCM_EnableAHBPeriphClock(IIC2_DMA_RTC);

    /* DMA config */
	  DMA_Reset(IIC_DMA_TX_Channel);  
    dmaConfig.peripheralBaseAddr = IIC_TX_ADDRESS;  
	  dmaConfig.memoryBaseAddr = (uint32_t)NULL; // 内存起始地址=null;初始内存地址不设置;
    dmaConfig.dir = DMA_DIR_PERIPHERAL_DST; // 内存到IIC外设
    dmaConfig.bufferSize = 0;  
    dmaConfig.peripheralInc = DMA_PERIPHERAL_INC_DISABLE;   
    dmaConfig.memoryInc = DMA_MEMORY_INC_ENABLE;  
    dmaConfig.peripheralDataSize = DMA_PERIPHERAL_DATA_SIZE_BYTE;  
    dmaConfig.memoryDataSize = DMA_MEMORY_DATA_SIZE_BYTE;  
    dmaConfig.loopMode = DMA_MODE_NORMAL; // 单次数据传递,传递完bufferSize个数据,就结束。   
    dmaConfig.priority = DMA_PRIORITY_HIGH;  
    dmaConfig.M2M = DMA_M2MEN_DISABLE;  
    
    /* Enable DMA channel */
    DMA_Config(IIC_DMA_TX_Channel, &dmaConfig);
		
		// 中断
		DMA_EnableInterrupt(IIC_DMA_TX_Channel, DMA_INT_TC);
		NVIC_EnableIRQRequest(I2C_Tx_DMA_Chanel_IRQn, 2, 2);
		
}

// I2C的DMA搬运数据启动：buffer内存数据搬到I2C外设寄存器中，并发送出去;
void i2c_dma_transmit_buffer(unsigned char *buffer, unsigned int length){
	  uint8_t ii = I2C_TimeOut;
    DMA_Disable(IIC_DMA_TX_Channel); // 先关闭DMA：方便设置DMA参数;
    IIC_DMA_TX_Channel->CHMADDR = (uint32_t)buffer; // DMA的dmaConfig.memoryBaseAddr设置;
	  //传播数据设置
    IIC_DMA_TX_Channel->CHNDATA = length; // DMA的dmaConfig.bufferSize设置;
	  //DMA启动数据发送
    DMA_Enable(IIC_DMA_TX_Channel); // 启动DMA数据传递;
	  // 等待数据发送完成;
    while((DMA_ReadStatusFlag(I2C_Tx_DMA_FLAG_TC) == RESET) && ii--){		}
		// printf(" %u ",ii);
}

// I2C向从设备OLed_Tx_Addr，写1个字节数据：适合写命令数据
// cmd == 1，则是发送一个指令；cmd == 0，发送数据;
void i2c_wr_byte(unsigned char byte,unsigned char cmd){
	  uint8_t ii = I2C_TimeOut;
	  // 产生起始信号
		I2C_EnableGenerateStart(IIC_ID);
	  // 等待EV5事件：检测起始信号是否成功
	  while((!I2C_ReadEventStatus(IIC_ID, I2C_EVENT_MASTER_MODE_SELECT)) && ii){
			  ii--;
		} 		
		// 发送从设备地址
	  I2C_Tx7BitAddress(IIC_ID, OLed_Tx_Addr, I2C_DIRECTION_TX);
	  // 检测EV6事件：等待从机地址是否发送完;
		ii = I2C_TimeOut;
		while ((!I2C_ReadEventStatus(IIC_ID,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) && ii){
			  ii--;
		} 	   
	  // 数据寄存器地址,或指令寄存器地址：数据模式;			
    if(cmd) 
		   I2C_TxData(IIC_ID, 0x00); 
    else
       I2C_TxData(IIC_ID, 0x40); 
    // 检测EV8：等待数据是否发送完;
		ii = I2C_TimeOut;
	  while ((!I2C_ReadEventStatus(IIC_ID, I2C_EVENT_MASTER_BYTE_TRANSMITTING)) && ii){
			  ii--;
		}  
		// 检测EV8_2；等待最后一个字节数据发送完; 
		I2C_TxData(IIC_ID, byte); 
		ii = I2C_TimeOut;
	  while ((!I2C_ReadEventStatus(IIC_ID, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) && ii){
			  ii--;
		} 
    // 发送停止信号
		I2C_EnableGenerateStop(IIC_ID);
}

extern volatile uint8_t IIC_TransmitOk;
// I2C向从设备OLed_Tx_Addr，写一个数组数据buffer，长度=length：适合写长数据
void i2c_write_buffer(unsigned char *buffer,unsigned int length){
    uint8_t ii = I2C_TimeOut;
		uint16_t delay = 50000;
		while(IIC_TransmitOk == 0 && delay)
		{
			delay--;
		}
		
//		if(delay == 0)
//		{
//			I2C_TX_DMA_Init();
//			IIC_init();
//		  return;
//		}
		
	  I2C_EnableGenerateStart(IIC_ID);
	  // EV5
	  while((!I2C_ReadEventStatus(IIC_ID, I2C_EVENT_MASTER_MODE_SELECT)) && ii--){  }   
	  I2C_Tx7BitAddress(IIC_ID, OLed_Tx_Addr, I2C_DIRECTION_TX);
	  // EV6
		ii = I2C_TimeOut;
		while((!I2C_ReadEventStatus(IIC_ID,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) && ii--){ 	} 
#if Upboard_IIC != 1		
	  I2C_TxData(IIC_ID, 0x40); 
#endif
		// EV8
		ii = I2C_TimeOut;
	  while ((!I2C_ReadEventStatus(IIC_ID, I2C_EVENT_MASTER_BYTE_TRANSMITTING)) && ii--){		}  
		IIC_TransmitOk = 0;
    i2c_dma_transmit_buffer(buffer,length);	 // DMA Transmit		
}

// IIC读数据：
uint8_t i2c_read_byte(unsigned char add){  
	  uint8_t readByte = 0;
	  uint8_t ii = I2C_TimeOut;
	  // 产生起始信号
		I2C_EnableGenerateStart(IIC_ID);
	  // 等待EV5事件：检测起始信号是否成功
	  while((!I2C_ReadEventStatus(IIC_ID, I2C_EVENT_MASTER_MODE_SELECT)) && ii--){	}    
		// 发送从设备地址
	  I2C_Tx7BitAddress(IIC_ID, OLed_Tx_Addr, I2C_DIRECTION_TX);
	  // 检测EV6事件：等待从机地址是否发送完;
		ii = I2C_TimeOut;
		while ((!I2C_ReadEventStatus(IIC_ID,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) && ii--){	} 	   
	  // 发送数据寄存器地址：
		I2C_TxData(IIC_ID, add); 
		// 检测EV8_2；等待最后一个字节数据发送完; 
		ii = I2C_TimeOut;
	  while ((!I2C_ReadEventStatus(IIC_ID, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) && ii--){ 	} 
		////////////////////////// 启动读数据系列 ////////////////////////////
		// 产生起始信号
		I2C_EnableGenerateStart(IIC_ID);
	  // 等待EV5事件：检测起始信号是否成功
		ii = I2C_TimeOut;
	  while((!I2C_ReadEventStatus(IIC_ID, I2C_EVENT_MASTER_MODE_SELECT)) && ii--){	}    
		// 发送从设备地址：读模式
	  I2C_Tx7BitAddress(IIC_ID, OLed_Tx_Addr, I2C_DIRECTION_RX);  
		// 关闭应答
		I2C_DisableAcknowledge(IIC_ID); 
    // 发送停止信号
		I2C_EnableGenerateStop(IIC_ID); 
		// 等待事件EV7:接收数据
		ii = I2C_TimeOut;
		while((!I2C_ReadEventStatus(IIC_ID, I2C_EVENT_MASTER_BYTE_RECEIVED)) && ii--){  } 
		readByte = I2C_RxData(IIC_ID);	  
		// 开启应答  
		I2C_EnableAcknowledge(IIC_ID);  
    return 	readByte;
}

#endif

